package cn.cuilan.hutool.core;

import cn.hutool.core.clone.CloneRuntimeException;
import cn.hutool.core.clone.CloneSupport;
import cn.hutool.core.clone.Cloneable;
import cn.hutool.core.util.ObjectUtil;

import java.io.Serializable;

/**
 * 对象克隆、深克隆
 *
 * @author zhangyan
 * @since 2023/1/4
 */
public class CloneTest {

    /**
     * 泛型克隆接口，Cloneable 接口方式实现
     */
    private static class Cat implements Cloneable<Cat> {
        @Override
        public Cat clone() {
            try {
                return (Cat) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new CloneRuntimeException(e);
            }
        }
    }

    /**
     * 泛型克隆类，CloneSupport 类方式继承
     */
    private static class Dog extends CloneSupport<Dog> implements Serializable {
        private String name = "旺";
        private int age = 3;
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        // 深克隆，前提是对象必须实现Serializable接口。
        Dog newDog = ObjectUtil.cloneByStream(dog);
        System.out.println(newDog.name + ":" + newDog.age);
    }

}
